package com.jbh.recordlibrary.v;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.google.gson.Gson;
import com.jbh.recordlibrary.u.ScreenUtil;

import java.util.ConcurrentModificationException;
import java.util.List;

public class RecordWaveView extends View {

    /**
     * 默认绘制宽度为屏幕宽
     */
    private int defaultWidth ;
    /**
     * 默认绘制高度
     *  100dp
     *
     * @param context
     */
    private int defaultHeight ;
    /**
     * 绘制的view的宽高
     */
    private int viewWidth;
    private int viewHeight ;

    /**
     * 默认背景
     */
    private int defaultBgColor = Color.WHITE;


    private Paint mPaint ;



    /**
     * 绘制音频图形颜色
     *
     */
    private int recordLineColor = Color.GRAY;
    private int recordLineWidth = 4;


    private int centerLineColor = Color.GRAY ;
    private int centerLineHeight = 1 ;


    public void setCenterLineColor(int centerLineColor) {
        this.centerLineColor = centerLineColor;
    }


    public void setCenterLineHeight(int centerLineHeight) {
        this.centerLineHeight = centerLineHeight;
    }

    public RecordWaveView(Context context) {
        this(context,null);
    }


    public RecordWaveView(Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RecordWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        defaultWidth = ScreenUtil.w(getContext());
        defaultHeight = ScreenUtil.dpToPx(getContext(),100);
        setBackgroundColor(defaultBgColor);
        initDrawRecordPaint();
    }

    private void initDrawRecordPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(recordLineColor);
        mPaint.setStrokeWidth(recordLineWidth);
    }



    /**
     * 测量view大小
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(viewWidth = measureWidth(widthMeasureSpec),viewHeight=measureHeight(heightMeasureSpec));
    }




    /**
     * 测量高度
     *
     * @param heightMeasureSpec
     *
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int tempHeight = 0 ;

        if (heightMode == MeasureSpec.EXACTLY){
            tempHeight = heightSize ;
        }else {
            if (MeasureSpec.AT_MOST == heightMode){
                tempHeight = Math.min(heightSize,defaultHeight);
            }
        }
        return tempHeight ;
    }


    /**
     * 测量宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int tempWidth = 0 ;
        if (widthMode == MeasureSpec.EXACTLY){
            tempWidth = widthSize ;
        }else {
            if (MeasureSpec.AT_MOST == widthMode){
                tempWidth = Math.min(widthSize,defaultWidth);
            }
        }
        return tempWidth ;
    }



    /****************************************/
    /**           录音绘制图形部分          **/
    /****************************************/


    private List<Short> totalBuf ;
    private float divider ;
    int lineWidth = 1 ;
    int lineColor = Color.GRAY ;



    /**
     *
     * @param totalBuf
     */
    public void setData(float divider,List<Short> totalBuf){
        this.totalBuf=totalBuf;
        this.divider=divider;
        Log.v("[RecordWaveView]","RecordWaveView = "+new Gson().toJson(totalBuf));
        invalidate();
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        if (isSupportDrawLine){
            drawTopLine(canvas);
            drawCenterLine(canvas);
            drawBottomLine(canvas);
        }
        if (totalBuf!=null){
            drawRecord(canvas,divider,totalBuf);
        }
        canvas.restore();
    }





    /**
     * 这里是绘制音频图形
     */
    public void drawRecord(final Canvas canvas, final float divider, final List<Short> buf){
        if (canvas!=null){
            try {
                //高低位交换
                for (int i = 0; i < buf.size(); i++) {
                    byte bus[] = getBytes(buf.get(i));
                    buf.set(i, (short)((0x0000 | bus[1]) << 8 | bus[0]));
                }

                final int baseLine = getHeight()/2 ;
                for (int i = 0; i < buf.size(); i++) {
                    double fb = buf.get(i) ;
                    if (buf.get(i)<=5){
                        fb = 5 ;
                    }
                    float radio = (float) (fb *1.0 / 100);
                    final float y = radio * baseLine;
                    /**当前指定位置绘制*/
                    final float currentDX = (i * divider)+2 ;
                    /**中间出波形*/
                    canvas.drawLine(currentDX, baseLine-y, currentDX,baseLine + y, mPaint);
                }
            }catch (ConcurrentModificationException exception){
                exception.printStackTrace();
            }
        }
    }



    /**
     * 中心线
     *
     * @param canvas
     */
    private void drawCenterLine(Canvas canvas) {
        Paint centerLine =new Paint();
        centerLine.setColor(lineColor);
        centerLine.setStrokeWidth(lineWidth);
        centerLine.setAntiAlias(true);
        centerLine.setDither(true);
        canvas.drawLine(0, getHeight()/2 - lineWidth/2, getWidth() ,getHeight()/2 + lineWidth/2, centerLine);
    }



    /**
     *
     * 最下面的那根线
     *
     * @param canvas
     */
    private void drawBottomLine(Canvas canvas) {
        Paint paintBottomLine =new Paint();
        paintBottomLine.setColor(lineColor);
        paintBottomLine.setStrokeWidth(lineWidth);
        paintBottomLine.setAntiAlias(true);
        paintBottomLine.setDither(true);
        canvas.drawLine(0, getHeight() - lineWidth, getWidth(), getHeight(), paintBottomLine);
    }



    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }



    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
    }



    private boolean isSupportDrawLine = true  ;

    public void setSupportDrawLine(boolean supportDrawLine) {
        isSupportDrawLine = supportDrawLine;
    }




    /**
     * 最上面的那根线
     *
     * @param canvas
     */
    private void drawTopLine(Canvas canvas) {
        Paint paintTopLine =new Paint();
        paintTopLine.setColor(lineColor);
        paintTopLine.setStrokeWidth(lineWidth);
        paintTopLine.setAntiAlias(true);
        paintTopLine.setDither(true);
        canvas.drawLine(0, 0, getWidth(), lineWidth, paintTopLine);
    }





    /**
     * 高低位交换
     *
     * @param s
     * @return
     */
    public byte[] getBytes(short s) {
        byte[] buf = new byte[2];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = (byte) (s & 0x00ff);
            s >>= 8;
        }
        return buf;
    }
}
